
function nettoyer_grille () {
	$('.player').each(function () {
		$(this).html('');
	});
	
	$('.move').each(function () {
		$(this).unbind();
		$(this).removeClass('move');
		$(this).removeClass('jumpp1');
		$(this).removeClass('jumpp2');
		$(this).addClass('empty');
	});
}

function select_p (x, y) {
	if (!$('#' + x + '_' + y).hasClass('player')) {
		$('.player').fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)
			.fadeOut(100).fadeIn(100);
	} else {
		if ($('#' + x + '_' + y).html() != '') {
			nettoyer_grille();
		} else {
			nettoyer_grille();
			
			$.ajax({
				type: "GET",
				url: "ajax/couppossibles",
				data: 'x=' + x + '&y=' + y,
				success: function(msg){
					$('.move').each(function (e) {
						e.removeClass('move');
					});
					eval(msg);
				}
			});
		}
	}
}

function tous (x, y) {
	nettoyer_grille();
	
	$.ajax({
		type: "GET",
		url: "ajax/couppossibles",
		success: function(msg){
			$('.move').each(function (e) {
				e.removeClass('move');
			});
			eval(msg);
		}
	});
}

function jouer_coup (xorig, yorig, xdest, ydest) {
	put_rest_resource ("ajax/coup", "xorig=" + xorig + "&yorig=" + yorig
			+ "&xdest=" + xdest + "&ydest=" + ydest);
}

function get_ajax_grille () {
	$.ajax({
		type: "GET",
		url: "ajax/grille",
		success: function(msg){
			eval(msg);
		}
	});
	
	$.ajax({
		type: "GET",
		url: "functions.js",
		success: function(msg){
			eval(msg);
		}
	});
	
	if ($('#joueur_' + current_player + '_auto').text() == 'X') {
		put_rest_resource('ajax/meilleurcoup');
	}
}

function toggle_auto (player_id) {
	if ($('#joueur_' + player_id + '_auto').text() == 'X') {
		$('#joueur_' + player_id + '_auto').text(' ');
	} else {
		$('#joueur_' + player_id + '_auto').text('X');
	}
	
	//~ get_ajax_grille();
}

function reset () {
	current_player = 0;
	delete_rest_resource('ajax/grille');
}

function put_rest_resource (resource_name, params) {
	$.ajax({
		type: "PUT",
		url: resource_name + "?" + params,
		async: false
	});
	
	get_ajax_grille();
}

function delete_rest_resource (resource_name) {
	$.ajax({
		type: "DELETE",
		url: resource_name,
		async: false
	});
	
	get_ajax_grille();
}

